package com.chenyitun.cloudadmin.sys.controller;

import com.chenyitun.cloudcommon.context.FilterContextHandler;
import com.chenyitun.cloudcommon.dto.MenuInfo;
import com.chenyitun.cloudcommon.utils.R;
import com.chenyitun.cloudadmin.common.annotation.Log;
import com.chenyitun.cloudadmin.common.controller.BaseController;
import com.chenyitun.cloudadmin.domain.Menu;
import com.chenyitun.cloudadmin.domain.Tree;
import com.chenyitun.cloudadmin.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author bootdo 1992lcg@163.com
 * @version V1.0
 */
@RequestMapping("/menu")
@RestController
public class MenuController extends BaseController {
    @Autowired
    MenuService menuService;

    @Log("访问菜单")
    @GetMapping("tree")
    Tree<Menu> tree(){
        return menuService.getTree();
    }
    @GetMapping
    List<Tree<Menu>>  list(){
        return menuService.getTree().getChildren();
    }

    @GetMapping("{id}")
    Menu get(@PathVariable("id") Long id) {
        Menu menu = menuService.get(id);
        return menu;
    }

    @GetMapping("list")
    List<Menu> list(@RequestParam Map<String, Object> params) {
        List<Menu> menus = menuService.list(params);
        return menus;
    }

    @PutMapping()
    R update(@RequestBody Menu Menu){
        if(menuService.update(Menu)>0){
            return R.ok();
        }
        return  R.error();
    }
    @PostMapping
    R save(@RequestBody Menu Menu){
        return R.operate(menuService.save(Menu)>0);
    }

    @DeleteMapping()
    R remove(Long id){
        if(menuService.remove(id)>0){
            return R.ok();
        }
        return R.error();
    }

    @GetMapping("userMenus")
    List<MenuInfo> userMenus(){
        List<Menu> MenuS = menuService.userMenus(Long.parseLong(FilterContextHandler.getUserID()));
//        List<Menu> MenuS = menuService.userMenus(1l);
        List<MenuInfo> MenuInfoS = new ArrayList<>();
        for (Menu Menu:MenuS){
            MenuInfo MenuInfo = new MenuInfo();
            MenuInfo.setMenuId(Menu.getMenuId());
            MenuInfo.setUrl(Menu.getUrl());
            MenuInfo.setPerms(Menu.getPerms());
            MenuInfoS.add(MenuInfo);
        }
        return MenuInfoS;
    }

    @GetMapping("clearCache")
    R clearCache(){
        Boolean flag = menuService.clearCache(Long.parseLong(FilterContextHandler.getUserID()));
        if (flag){
            return  R.ok();
        }
        return R.error();
    }

    /**
     * 当前用户菜单的树形结构
     * @return
     */
    @RequestMapping("/currentUserMenus")
    List<Tree<Menu>> currentUserMenus() {
        List<Tree<Menu>> menus = menuService.listMenuTree(Long.parseLong(FilterContextHandler.getUserID()));
        return menus;
    }

    @GetMapping("/roleId")
    List<Long> menuIdsByRoleId(Long roleId){
        return menuService.MenuIdsByRoleId(roleId);
    }
}
